package com.aiden.cashbook.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory

object BitmapUtils {

    fun decodeBitmap(context: Context, resId: Int, width: Int, height: Int): Bitmap {
        var inputStream = context.resources.openRawResource(resId)

        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true

        val bitmap = BitmapFactory.decodeStream(inputStream, null, options)

        val wRadio = (options.outWidth / width)
        val hRadio = (options.outHeight / height)

        if (wRadio > 1 && hRadio > 1) {
            if (wRadio > hRadio) {
                options.inSampleSize = wRadio
            } else {
                options.inSampleSize = hRadio
            }
        }
        inputStream = context.resources.openRawResource(resId)
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeStream(inputStream, null, options)
    }
}